import { createApp } from 'vue';
import { createPinia } from 'pinia';
import api from './api/api';
// import Antd from 'ant-design-vue'; 按需导入
import 'ant-design-vue/dist/reset.css'; // 保留样式文件
import App from './App.vue';
import router from './router';
import hasPermission from './directives/hasPermission';
import { iconNames } from './config/iconNames';
import * as AntdIcons from '@ant-design/icons-vue';

const app = createApp(App);
// 使用 Pinia 状态管理
const pinia = createPinia();

// 注册图标
iconNames.forEach((iconName) => {
  const icon = AntdIcons[iconName];
  if (icon) {
    app.component(iconName, icon);
  }
});

// 提供 API
app.provide('$api', api);
app.directive('hasPermission', hasPermission);

// 参数跳转指令
app.directive('param', {
  mounted(el, binding) {
    const id = binding.value; // 获取传入的 ids
    if (!id) {
      return;
    }
    el.addEventListener('click', () => {
      const prefixUrl = window.location.href.split('#')[0]
      const url = `${prefixUrl}${router.resolve({ name: 'EquipParam', query: { id } }).href}`
      window.open(url, '_blank')
    });
  },
  beforeUnmount(el) {
    el.removeEventListener('click', () => { });
  },
});

app.config.warnHandler = () => null;
app
  .use(pinia)
  .use(router)
  .mount('#app');